﻿
using Extentions;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using NPOI.SS.Formula.Functions;
using Senparc.Weixin.Open.WxOpenAPIs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using TrainingReminderSystem.Dal;
using TrainingReminderSystem.Model;
using static NPOI.HSSF.Util.HSSFColor;

namespace TrainingReminderSystem.Bll
{
    /// <summary>
    /// 逻辑层
    /// </summary>
    public partial class SysCustomerBll : CustomerLoginedBll
    {
        #region 查询条件

        protected override Expression<Func<T, bool>> InitSearchDelegetePartial<T, TDbContext>(Expression<Func<T, bool>> tWhere, string key, string val, TDbContext tDb)
        {
            if (key == "HasWechatH5")
            {
                Expression<Func<SysCustomer, bool>> where = tWhere as Expression<Func<SysCustomer, bool>>;
                var temp = val.ExToBool();
                if (temp)
                {
                    where = where.ExAnd(m => m.SysCustomerOpenID.Any(n => n.EnumOpenIDType == EnumOpenIDType.微信公众号));
                    return where as Expression<Func<T, bool>>;
                }
                else
                {
                    where = where.ExAnd(m => m.SysCustomerOpenID.Any(n => n.EnumOpenIDType == EnumOpenIDType.微信公众号) == false);
                    return where as Expression<Func<T, bool>>;
                }
            }
            if (key == "HasWechatMini")
            {
                Expression<Func<SysCustomer, bool>> where = tWhere as Expression<Func<SysCustomer, bool>>;
                var temp = val.ExToBool();
                if (temp)
                {
                    where = where.ExAnd(m => m.SysCustomerOpenID.Any(n => n.EnumOpenIDType == EnumOpenIDType.微信小程序));
                    return where as Expression<Func<T, bool>>;
                }
                else
                {
                    where = where.ExAnd(m => m.SysCustomerOpenID.Any(n => n.EnumOpenIDType == EnumOpenIDType.微信小程序) == false);
                    return where as Expression<Func<T, bool>>;
                }
            }
            if (key == "maxRemindTime")
            {
                int day = val.ExToInt32();
                if (day > 0)
                {
                    Expression<Func<SysCustomer, bool>> where = tWhere as Expression<Func<SysCustomer, bool>>;
                    where = where.ExAnd(m => (m.CertSearch02Json != null || m.CertSearch03Json != null)
                    && (m.Json02MinTime1 != null || m.Json02MinTime2 != null || m.Json03MinTime1 != null || m.Json03MinTime2 != null));
                    DateTime dt = DateTime.Now.Date.AddDays(day);
                    where = where.ExAnd(m => m.Json02MinTime1 < dt || m.Json02MinTime2 < dt || m.Json03MinTime1 < dt || m.Json03MinTime2 < dt);
                    return where as Expression<Func<T, bool>>;
                }
            }

            return base.InitSearchDelegetePartial(tWhere, key, val, tDb);
        }

        #endregion

        #region 修改部分信息

        protected override AjaxResult<T> GetModelReturnPartial<T, TDbContext>(T tDbModel, TDbContext tDb) where T : class
        {
            var model = tDbModel as SysCustomer;
            var db = tDb as DbEntities;
            model.SysCustomerIdCard = db.SysCustomerIdCard.Where(m => m.SysCustomerID == model.SysCustomerID).ToList();
            model.SysCustomerOrganizationTrain = db.SysCustomerOrganizationTrain.Where(m => m.SysCustomerID == model.SysCustomerID).ToList();
            return AjaxResult<T>.SuccessResult(tDbModel);
        }

        protected override AjaxResult<dynamic> ValidatePartial<T, TDbContext>(T tModel, bool isAdd, AjaxResult<dynamic> r, TDbContext tDb)
        {
            var model = tModel as SysCustomer;
            var db = tDb as DbEntities;
            if (db.SysCustomer.Any(m => m.Phone == model.Phone && m.SysCustomerID != model.SysCustomerID))
            {
                string err = string.Format(comLan["Exists"], $"{keyLan["Phone"]}[{model.Phone}]");
                return AjaxResult<dynamic>.ErrorResult(err);
            }
            if (model.SysCustomerIdCard != null)
            {
                for (var i = 0; i < model.SysCustomerIdCard.Count; i++)
                {
                    var item = model.SysCustomerIdCard[i];
                    if (item == null)
                    {
                        model.SysCustomerIdCard.RemoveAt(i);
                        i--;
                        continue;
                    }
                    item.SysCustomerID = model.SysCustomerID;
                    item.CreateSysEmployeeID = PcLoginedBll.CurrentEmployee?.SysEmployeeID;
                    item.CreateDate = DateTime.Now;
                    item.IdCard = item.IdCard.ExTrim();
                }
            }
            if (model.SysCustomerOrganizationTrain != null)
            {
                for (var i = 0; i < model.SysCustomerOrganizationTrain.Count; i++)
                {
                    var item = model.SysCustomerOrganizationTrain[i];
                    if (item == null)
                    {
                        model.SysCustomerOrganizationTrain.RemoveAt(i);
                        i--;
                        continue;
                    }
                    item.SysCustomerID = model.SysCustomerID;
                    item.CreateSysEmployeeID = PcLoginedBll.CurrentEmployee?.SysEmployeeID;
                    item.CreateDate = DateTime.Now;
                }
            }
            return base.ValidatePartial(tModel, isAdd, r, tDb);
        }

        protected override AjaxResult<dynamic> AddUpdateSuccessPartial<T, TDbContext>(T tModel, bool isAdd, AjaxResult<dynamic> r, TDbContext tDb, T tDbModel = null)
        {
            var model = tModel as SysCustomer;
            var db = tDb as DbEntities;
            if (isAdd == false)
            {
                //证件号
                var dbSysCustomerIdCard = db.SysCustomerIdCard.Where(m => m.SysCustomerID == model.SysCustomerID).ToList();
                ChangeDetail<SysCustomerIdCard>(db, dbSysCustomerIdCard, model.SysCustomerIdCard,
                    (SysCustomerIdCard dbItem, SysCustomerIdCard submitItem) =>//改变属性
                    {
                        var listNotChange = new string[]
                        {
                            nameof(SysCustomerIdCard.CreateDate), nameof(SysCustomerIdCard.CreateSysEmployeeID)
                        };
                        dbItem.ExCopyFrom(submitItem, listNotChange);
                        //一定要挂上关系,不然会被移除掉
                        dbItem.SysCustomer = tDbModel as SysCustomer;
                    },
                    (SysCustomerIdCard dbItem, SysCustomerIdCard submitItem) =>//比较方法
                    {
                        return dbItem.ID == submitItem.ID;
                    },
                    (List<SysCustomerIdCard> addList, List<SysCustomerIdCard> updateList, List<SysCustomerIdCard> removeList) =>
                    {
                    });

                //证件号
                var dbSysCustomerOrganizationTrain = db.SysCustomerOrganizationTrain.Where(m => m.SysCustomerID == model.SysCustomerID).ToList();
                ChangeDetail<SysCustomerOrganizationTrain>(db, dbSysCustomerOrganizationTrain, model.SysCustomerOrganizationTrain,
                    (SysCustomerOrganizationTrain dbItem, SysCustomerOrganizationTrain submitItem) =>//改变属性
                    {
                        var listNotChange = new string[]
                        {
                            nameof(SysCustomerOrganizationTrain.CreateDate), nameof(SysCustomerOrganizationTrain.CreateSysEmployeeID)
                        };
                        dbItem.ExCopyFrom(submitItem, listNotChange);
                        //一定要挂上关系,不然会被移除掉
                        dbItem.SysCustomer = tDbModel as SysCustomer;
                    },
                    (SysCustomerOrganizationTrain dbItem, SysCustomerOrganizationTrain submitItem) =>//比较方法
                    {
                        return dbItem.ID == submitItem.ID;
                    },
                    (List<SysCustomerOrganizationTrain> addList, List<SysCustomerOrganizationTrain> updateList, List<SysCustomerOrganizationTrain> removeList) =>
                    {
                    });
            }
            return base.AddUpdateSuccessPartial(tModel, isAdd, r, tDb, tDbModel);
        }

        #endregion

        #region 验证码Key

        /// <summary>
        /// 验证码Key
        /// </summary>
        private const string CaptchaKey = "Captcha";

        #endregion

        #region 登录/注销

        /// <summary>
        /// 登录
        /// 支持手机号和客户编号
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="pwd"></param>
        /// <param name="captchaText">验证码</param>
        /// <returns></returns>
        public AjaxResult<List<CustomerLoginInfo>> Login(string uid, string pwd, string captchaText)
        {
            var r = new AjaxResult<List<CustomerLoginInfo>>();
            if (string.IsNullOrEmpty(uid))
            {
                r.Message = LanguageHelper.GetString<SysEmployee>("uid");
                return r;
            }
            if (string.IsNullOrEmpty(pwd))
            {
                r.Message = LanguageHelper.GetString<SysEmployee>("pwd");
                return r;
            }

            if (SysConfig.NeedCaptcha)
            {
                if (string.IsNullOrEmpty(captchaText))
                {
                    r.Message = lan["captchaText"];
                    return r;
                }

                captchaText = captchaText.ToUpper();
                string captcha = CacheHelper.GetCache(CaptchaKey + Device) as string;
                if (captcha != captchaText)
                {
                    CacheHelper.RemoveCache(CaptchaKey + Device);
                    r.Message = lan["captchaError"];
                    return r;
                }
            }
            pwd = pwd.ToUpper();

            using var db = DbNoFilter;
            //帐号是在客户里找的
            r = LoginBase(m => m.Phone == uid, pwd, null, null, db);
            if (SysConfig.NeedCaptcha && r.Success)
            {
                CacheHelper.RemoveCache(CaptchaKey + Device);
            }
            return r;
        }

        /// <summary>
        /// 设置登录信息
        /// </summary>
        /// <param name="token"></param>
        /// <param name="validateLoginOutTime">是否验证失效时间</param>
        /// <returns></returns>
        public AjaxResult<CustomerLoginInfo> LoginByToken(string token, bool validateLoginOutTime = false)
        {
            using (var db = DbNoFilter)
            {
                return SetCacheByToken(token, db);
            }
        }

        #region 登录辅助方法

        /// <summary>
        /// 登录基础方法
        /// </summary>
        /// <param name="where"></param>
        /// <param name="pwd">密码登录的记录日志,进行密码判断</param>
        /// <param name="token"></param>
        /// <param name="wechatToken"></param>
        /// <param name="db"></param>
        /// <param name="withCache"></param>
        /// <returns></returns>
        internal AjaxResult<List<CustomerLoginInfo>> LoginBase(Expression<Func<SysCustomer, bool>> where, string pwd, string token, string wechatToken, DbEntities db, bool withCache = true)
        {
            //是否是密码登录的,如果是,则要做日志记录的
            bool isPwdLogin = !string.IsNullOrEmpty(pwd);
            var r = new AjaxResult<List<CustomerLoginInfo>>();
            IQueryable<SysCustomer> sysCustomerListTemp = GetSysCustomerListDb(where, db);
            if (sysCustomerListTemp.Any() == false)
            {
                r.Message = LanguageHelper.GetString<SysEmployee>("notExist");
                return r;
            }
            string errorKey = null;
            List<SysCustomer> listDb = sysCustomerListTemp.ToList();
            foreach (SysCustomer item in listDb)
            {
                if (isPwdLogin)
                {
                    if (string.IsNullOrEmpty(item.Pwd))
                    {
                        item.Pwd = DEncryptHelper.MakeMd5("123456");
                    }
                    if (item.Pwd != pwd)
                    {
                        errorKey = "pwdError";
                        continue;
                    }
                }

                errorKey = item.ValidateLogin();
                if (!string.IsNullOrEmpty(errorKey)) continue;
                //成功了的
                errorKey = null;
                break;
            }
            if (!string.IsNullOrEmpty(errorKey))
            {
                r.Message = LanguageHelper.GetString<SysEmployee>(errorKey);
                return r;
            }
            //以上面循环的提示逻辑做为筛选,筛选正常的数据集合
            Func<SysCustomer, bool> validateLoginWhere = SysCustomer.GetValidateLoginWhere();
            listDb = listDb.Where(validateLoginWhere).ToList();
            if (isPwdLogin)
            {
                Func<SysCustomer, bool> validateLoginPwdWhere = SysCustomer.GetValidateLoginPwdWhere(pwd);
                listDb = listDb.Where(validateLoginPwdWhere).ToList();
            }
            if (listDb.Count == 0)
            {
                r.Message = LanguageHelper.GetString<SysEmployee>("logicError");
                return r;
            }
            //带密码的,就是登录了的
            if (isPwdLogin)
            {
                string error = AddLog(db, listDb[0], EnumLogType.登录);
                if (!string.IsNullOrEmpty(error))
                {
                    r.Message = error;
                    r.Success = false;
                    return r;
                }
            }
            List<CustomerLoginInfo> listResult = new List<CustomerLoginInfo>();
            //如果有多个客户信息
            foreach (SysCustomer customer in listDb)
            {
                if (string.IsNullOrEmpty(token))
                {
                    token = CreateToken(customer);
                }
                //设置权限和客户信息缓存
                AjaxResult<CustomerLoginInfo> temp = SetCacheByToken(token, db, customer, wechatToken, withCache);
                if (temp.Success == false)
                {
                    r.Total = (int)EnumTotal.Token错误;
                    r.Message = temp.Message;
                    return r;
                }
                listResult.Add(temp.Result);
                token = null;
            }
            db.SaveChanges();
            r.Success = true;
            r.Result = listResult;
            if (listResult.Count == 1)
            {
                SysCustomerOpenIDBll.Bind(listResult[0].SysCustomerID, db);
            }
            return r;
        }

        private IQueryable<SysCustomer> GetSysCustomerListDb(Expression<Func<SysCustomer, bool>> where, DbEntities db)
        {
            var listTemp = db.SysCustomer.AsNoTracking().Include(m => m.SysStore);
            var list = listTemp.Where(where);
            return list;
        }

        private string CreateToken(SysCustomer customer)
        {
            string loginUidTemp = !string.IsNullOrEmpty(customer.Phone)
                ? customer.Phone
                : customer.SysCustomerName;
            string tokenInfo =
                $"{customer.SysCustomerID}{TokenSplit}{customer.GetCurrentStore().SysStoreID}{TokenSplit}{(int)SysConfig.EnumRunProject}{TokenSplit}{loginUidTemp}{TokenSplit}{CustomerTokenKey}{TokenSplit}{DateTime.Now.ExToNormal()}";
            var r = customer.SysCustomerID + TokenSplit + DEncryptHelper.Encrypt(tokenInfo);
            return r;
        }

        /// <summary>
        /// 设置缓存信息
        /// </summary>
        /// <param name="token"></param>
        /// <param name="db"></param>
        /// <param name="customer"></param>
        /// <param name="wechatToken"></param>
        /// <param name="withCache"></param>
        /// <returns></returns>
        private AjaxResult<CustomerLoginInfo> SetCacheByToken(string token, DbEntities db, SysCustomer customer = null, string wechatToken = null, bool withCache = true)
        {
            var r = new AjaxResult<CustomerLoginInfo>();
            if (string.IsNullOrEmpty(token))
            {
                //解析失败的情况
                r.Message = LanguageHelper.GetString<SysEmployee>("tokenError");
                return r;
            }
            //有token解析他,他有具体是某一个帐号的
            var tempArray = token.Split(TokenSplit);
            var encrypt = token.Length > 1 ? tempArray[1] : null;
            if (string.IsNullOrEmpty(encrypt))
            {
                //解析失败的情况
                r.Message = LanguageHelper.GetString<SysEmployee>("tokenError");
                return r;
            }
            string val = DEncryptHelper.Decrypt(encrypt);
            if (string.IsNullOrEmpty(val))
            {
                //解析失败的情况
                r.Message = LanguageHelper.GetString<SysEmployee>("tokenError");
                return r;
            }
            var arr = val.Split(TokenSplit);
            if (arr.Length < 6)
            {
                //解析失败的情况
                r.Message = LanguageHelper.GetString<SysEmployee>("tokenError");
                return r;
            }
            int id = arr[0].ExToInt32();
            if (id <= 0)
            {
                //解析失败的情况
                r.Message = LanguageHelper.GetString<SysEmployee>("tokenError");
                return r;
            }
            if (tempArray[0].ExToInt32() != id)
            {
                //解析失败的情况
                r.Message = LanguageHelper.GetString<SysEmployee>("tokenError");
                return r;
            }
            //店铺ID
            int sysStoreID = arr[1].ExToInt32();
            if (sysStoreID <= 0)
            {
                //解析失败的情况
                r.Message = LanguageHelper.GetString<SysEmployee>("tokenError");
                return r;
            }
            //最后一个是时间
            DateTime dt = arr[arr.Length - 1].ExToDateTime();//arr[^1]最后一个的最新写法
            if (dt.Date != DateTime.Now.Date)
            {
                //解析失败的情况
                r.Message = LanguageHelper.GetString<SysEmployee>("tokenOverDate");
                return r;
            }
            //if (validateLoginOutTime)
            //{
            //    //店铺名称获取到的店铺ID
            //    var setting = SysSettingBll.GetModel(EnumSettingCode.PC端登录失效时间, sysStoreID, db);
            //    int timeOut = setting != null ? setting.Value.ExToInt32() : 0;
            //    if (timeOut > 0)
            //    {
            //        int splitMin = (DateTime.Now - dt).Minutes;
            //        if (splitMin >= timeOut)
            //        {
            //            return r;
            //        }
            //    }
            //}

            if (customer == null)
            {
                IQueryable<SysCustomer> sysCustomerListTemp = GetSysCustomerListDb(m => m.SysCustomerID == id, db);
                customer = sysCustomerListTemp.FirstOrDefault();
                if (customer == null)
                {
                    //解析失败的情况
                    r.Message = LanguageHelper.GetString<SysEmployee>("logicError");
                    return r;
                }
                string errorKey = customer.ValidateLogin();
                if (!string.IsNullOrEmpty(errorKey))
                {
                    r.Message = LanguageHelper.GetString<SysEmployee>(errorKey);
                    return r;
                }
            }

            SysStore store = customer.GetCurrentStore();
            if (withCache)
            {
                SetCurrentCustomer(token, customer);
            }

            CustomerLoginInfo result = new CustomerLoginInfo()
            {
                WechatToken = wechatToken,

                Token = token,
                UserName = customer.SysCustomerName,
                UserHeader = customer.HeaderUrl,

                SysCustomerID = customer.SysCustomerID,
                SysStoreID = store.SysStoreID,
                SysStoreName = store.SysStoreName,

                IsDemo = customer.IsDemo,
                CreateDate = DateTime.Now,

                EnumSysCustomerType = customer.EnumSysCustomerType,
            };

            r.Success = true;
            r.Result = result;
            return r;
        }

        #endregion

        /// <summary>
        /// 注销登录
        /// </summary>
        /// <returns></returns>
        public AjaxResult<CustomerLoginOutResult> LoginOut()
        {
            string token = Token;
            if (string.IsNullOrEmpty(token)) return AjaxResult<CustomerLoginOutResult>.SuccessResult();
            int sysCustomerID = CurrentCustomer?.SysCustomerID ?? 0;
            int sysStoreID = CurrentStore?.SysStoreID ?? 0;
            if (sysCustomerID > 0)
            {
                using var db = DbNoFilter;
                //string error = 
                AddLog(db, new SysCustomer()
                {
                    SysCustomerID = sysCustomerID
                }, EnumLogType.注销);
                //if (!string.IsNullOrEmpty(error))
                //{
                //    return AjaxResult<LoginOutResult>.ErrorResult(error);
                //}
                db.SaveChanges();
                SysCustomerOpenIDBll.UnBind(db);
            }

            ClearCache(token);
            WebSocketBll.CloseThis(token);
            return AjaxResult<CustomerLoginOutResult>.SuccessResult(new CustomerLoginOutResult(sysCustomerID, sysStoreID));
        }

        /// <summary>
        /// 注销结果
        /// </summary>
        public class CustomerLoginOutResult
        {
            public CustomerLoginOutResult(int sysCustomerID, int sysStoreID)
            {
                SysCustomerID = sysCustomerID;
                SysStoreID = sysStoreID;
            }

            /// <summary>
            /// 客户ID
            /// </summary>
            public int SysCustomerID { get; set; }

            /// <summary>
            /// 店铺ID
            /// </summary>
            public int SysStoreID { get; set; }
        }

        #endregion

        #region 修改密码

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="customerID"></param>
        /// <param name="oldPass"></param>
        /// <param name="pass"></param>
        /// <param name="withOutOldPass">是否判断旧密码</param>
        /// <returns></returns>
        public AjaxResult<int> UpdatePwd(int customerID, string oldPass, string pass, bool withOutOldPass = false)
        {
            if (CurrentCustomer != null && CurrentCustomer.IsDemo)
            {
                return AjaxResult<int>.ErrorResult("试用帐号不能更改密码");
            }
            oldPass = oldPass.ExTrim().ToUpper();
            pass = pass.ExTrim().ToUpper();
            var r = new AjaxResult<int>();
            if (oldPass == pass)
            {
                r.Message = LanguageHelper.GetString<SysEmployee>("pwdEqual");
                return r;
            }

            using (var db = Db)
            {
                var beforeModel = db.SysCustomer.FirstOrDefault(m => m.SysCustomerID == customerID);
                if (beforeModel == null)
                {
                    r.Message = LanguageHelper.NoData;
                    return r;
                }
                if (withOutOldPass == false)
                {
                    if (beforeModel.Pwd != oldPass)
                    {
                        r.Message = "旧密码错误,不能更改";
                        return r;
                    }
                }

                beforeModel.Pwd = pass;
                string error = AddLog(db, beforeModel, EnumLogType.编辑);
                if (!string.IsNullOrEmpty(error))
                {
                    return AjaxResult<int>.ErrorResult(error);
                }
                db.SaveChanges();

                r.Message = LanguageHelper.Success;
                r.Success = true;
                return r;
            }
        }

        #endregion

        #region 我的信息

        /// <summary>
        /// 我的信息
        /// </summary>
        /// <returns></returns>
        public AjaxResult<dynamic> GetMyInfo()
        {
            var r = new AjaxResult<dynamic>();
            var current = CurrentCustomer;
            using (var db = Db)
            {
                r.Result = new
                {
                    current.SysCustomerName,
                    current.Phone,
                    current.HeaderUrl,
                    current.IsDemo,
                    LastLoginDateTime = db.SysOperateLog.OrderByDescending(m => m.ID).Where(m => m.EnumOperateLogType == EnumOperateLogType.登录 && m.SysCustomerID == current.SysCustomerID).Select(m => (DateTime?)m.CreateDate).FirstOrDefault()
                };
            }
            r.Success = true;
            return r;
        }

        /// <summary>
        /// 修改我的信息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="phone"></param>
        /// <param name="headerUrl"></param>
        /// <returns></returns>
        public AjaxResult<dynamic> UpdateMyInfo(string name, string phone, string headerUrl)
        {
            if (CurrentCustomer != null && CurrentCustomer.IsDemo)
            {
                return AjaxResult<dynamic>.ErrorResult("试用帐号不能更改信息");
            }
            var r = new AjaxResult<dynamic>();
            name = name.ExTrim();
            if (string.IsNullOrEmpty(name))
            {
                r.Message = "请输入客户姓名";
                return r;
            }

            phone = phone.ExTrim();
            if (string.IsNullOrEmpty(phone))
            {
                r.Message = "请输入手机号";
                return r;
            }
            headerUrl = headerUrl.ExTrim();
            if (!string.IsNullOrEmpty(headerUrl))
            {
                headerUrl = Extentions.Base64Helper.SaveFileFromBase64(headerUrl);
            }

            var current = CurrentCustomer;
            using (var db = Db)
            {
                if (db.SysCustomer.Any(m =>
                    m.Phone == phone && m.SysCustomerID != current.SysCustomerID))
                {
                    r.Message = "您输入的手机号已存在,请换个手机号";
                    return r;
                }

                var before = db.SysCustomer.FirstOrDefault(m => m.SysCustomerID == current.SysCustomerID);
                if (before == null)
                {
                    r.Message = LanguageHelper.NoData;
                    return r;
                }
                before.SysCustomerName = name;
                before.Phone = phone;
                before.HeaderUrl = headerUrl;

                string error = AddLog(db, before, EnumLogType.编辑);
                if (!string.IsNullOrEmpty(error))
                {
                    return AjaxResult<dynamic>.ErrorResult(error);
                }
                db.SaveChanges();
                current.SysCustomerName = name;
                current.Phone = phone;
                current.HeaderUrl = headerUrl;
                SetCurrentCustomer(current.Token, current);

                ClearCache(before.SysCustomerID);

                r.Result = headerUrl;
                r.Message = LanguageHelper.Success;
                r.Success = true;
                r.Total = (int)EnumTotal.用户信息及权限重新加载;
                return r;
            }
        }

        #endregion
    }

    #region 登录成功返回信息

    /// <summary>
    /// 登录成功返回信息
    /// </summary>
    public class CustomerLoginInfo
    {
        /// <summary>
        /// token信息
        /// </summary>
        public string Token { get; set; }

        /// <summary>
        /// 微信Token
        /// </summary>
        public string WechatToken { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 头像
        /// </summary>
        public string UserHeader { get; set; }

        /// <summary>
        /// 店铺名称
        /// </summary>
        public string SysStoreName { get; set; }

        /// <summary>
        /// 客户ID
        /// </summary>
        public int SysCustomerID { get; set; }

        /// <summary>
        /// 店铺ID
        /// </summary>
        public int SysStoreID { get; set; }

        /// <summary>
        /// 是否试用帐号
        /// </summary>
        public bool IsDemo { get; set; }

        /// <summary>
        /// 创建事件
        /// </summary>
        public DateTime CreateDate { get; set; }

        /// <summary>
        /// 客户类型
        /// </summary>
        public EnumSysCustomerType EnumSysCustomerType { get; set; }
    }

    #endregion
}
